<?php
class cyitemControl extends skymvc{
	public $userid;
	public function __construct(){
		parent::__construct();	
	}
	
	public function onInit(){
		if(in_array(get('a'),array("my","save","add","mylove"))){
			M("login")->checklogin();
		}
		$this->userid=M("login")->userid;
	}
	
	function onDefault(){
		$rscount=true;
		 
		$where=" status=2 AND siteid=".SITEID;
		$url="/index.php?m=cyitem";
		switch(get('s_status')){
			
			case 2:
				$where.=" AND issuccess=1 ";
				break;
			case 3:
				$where.=" AND endtime<".time()."";
				break;
			default:
				$where.=" AND endtime>=".time()."";
				break;
		}
		$item_status=get('item_status','i');
		if($item_status){
			$where.=" AND item_status=".$item_status;
			$url.="&item_status=".$item_status;
		}
		switch(get('orderby')){
			
			case "buy_num":
					$order=" buy_num DESC";
				break;
			case "money":
					$order=" money DESC";
				break;
			default:
					$order=" dateline DESC";
				break;
		}
		$start=get_post('per_page','i');
		$limit=20;
		$option=array(
			"where"=>$where,
			"start"=>$start,
			"limit"=>$limit,
			"order"=>$order
		);
		$data=M("cyitem")->select($option,$rscount);
		if($data){
			$t_ids=array();
			foreach($data as $k=>$v){
				$t_ids[]=$v['catid'];
				$t_uids[]=$v['userid'];
			}
			if($t_ids){
				$t_c=M("category")->cat_list(" catid in("._implode($t_ids).")");
			}
			
			if($t_uids){
				$t_u=M("user")->getUserByIds($t_uids);
			}
			foreach($data as $k=>$v){
				$v['cname']=$t_c[$v['catid']];
				if(isset($t_u[$v['userid']])){
					$v['nickname']=$t_u[$v['userid']]['nickname'];
				}
				$data[$k]=$v;
			}
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$this->smarty->goassign(array(
			"list"=>$data,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist
		));

	 
		$tpl=M("pagetpl")->get("cyitem","index","cyitem/index.html");
		$this->smarty->display($tpl);
	}
	
	public function onJoin(){
		M("login")->checkLogin();
		$cyid=get('cyid','i');
		$cyitem=M("cyitem")->selectRow("id=".$cyid);
		if(empty($cyitem)){
			$this->goAll("数据出错");
		}
		$data=M("cyitem_join")->selectRow("cyid=".$cyid." AND userid=".M("login")->userid);
		$this->smarty->goassign(array(
			"cyitem"=>$cyitem,
			"data"=>$data
		));
		$tpl=M("pagetpl")->get("cyitem","join");
		$this->smarty->display($tpl);
		$this->smarty->display("cyitem/join.html");
	}
	
	public function onJoinSave(){
		M("login")->checkLogin(1);
		$data=M("cyitem_join")->postData();
		$cyid=get_post('cyid','cyid'); 
		$row=M("cyitem_join")->selectRow("cyid=".$cyid." AND userid=".M("login")->userid);
		
		 
		
		if(empty($row)){
			$data['userid']=M("login")->userid;
			$data['siteid']=SITEID;
			$data['dateline']=time();
			M("cyitem_join")->insert($data);
			
			
			
		}else{
			M("cyitem_join")->update($data,"id=".$row['id']);
		}
		$c=M("cyitem_join")->selectOne(array(
				"where"=>" cyid=".$cyid,
				"fields"=>" count(*) "
			));
		M("cyitem")->update(array(
				"join_num"=>$c
			),"id=".$data['cyid']);
			 
		$this->goAll("success");
	}
	 
	/*我的创业项目模型*/
	public function onMy(){
		M("login")->checklogin();
		$limit=20;
		$where="  siteid=".SITEID." AND status<99 AND userid=".M("login")->userid;;
		$url="/index.php?m=cyitem&a=my";
		$user_catid=get('user_catid','i');
		if($user_catid){
			$ucids=M("user_category")->id_family($user_catid);
			$where.=" AND user_catid in("._implode($ucids).") ";
			$url.="&user_catid=".$user_catid;
		}
		$start=get('per_page','i');
		$option=array(
			"start"=>$start,
			"limit"=>$limit,
			"order"=>"id DESC",
			"where"=>$where,
		);
		$rscount=true;
		$data=M("cyitem")->select($option,$rscount);
		if($data){
			foreach($data as $k=>$v){
				$data[$k]['cname']=M("category")->selectOne(array("where"=>array("catid"=>$v['catid']),"fields"=>"cname"));
				if($v['catid_2nd']){
					$data[$k]['cname_2nd']=M("category")->selectOne(array("where"=>array("catid"=>$v['catid_2nd']),"fields"=>"cname"));
				}
				
				if($v['catid_3nd']){
					$data[$k]['cname_3nd']=M("category")->selectOne(array("where"=>array("catid"=>$v['catid_3nd']),"fields"=>"cname"));
				}
				
				
			}
		}
		 
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$this->smarty->goassign(
			array(
				"data"=>$data,
				"rscount"=>$rscount,
				"pagelist"=>$pagelist,
				"user_cat_list"=>M("user_category")->id_title(array("where"=>"userid=".M("login")->userid)),
				"pid_list"=>M("user_category")->children(array("where"=>" tablename='cyitem' AND pid=0 AND userid=".M("login")->userid))
			)
		);
		$tpl=M("pagetpl")->get("cyitem","my");
		$this->smarty->display($tpl);
		$this->smarty->display("cyitem/my.html");
	}
	
	public function onMyLove(){
		
		$userid=M("login")->userid;
		$limit=20;
		$start=get_post("per_page","i");
		$option=array(
			"limit"=>$limit,
			"start"=>$start,
			"order"=>" id DESC",
			"where"=>" userid=".$userid." AND tablename='cyitem' ",
		);
		$rscount=true;
		$data=M("love")->select($option,$rscount);
		 
		if($data){
			foreach($data as $k=>$v){
				$a=M("cyitem")->selectRow(" id=".$v['object_id']."");
				if(empty($a) or $a['status']>98 ){
					M("love")->delete("id=".$v['id']);;
				}else{
					$sdata[]=$a;
				}
			}
		}
		$url="/index.php?m=cyitem&a=mylove";
		$pagelist=$this->pagelist($rscount,$limit,$url); 
		$this->smarty->goassign(array(
			"data"=>$sdata,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist,
			"item_status_list"=>M("cyitem")->item_status_list()
		));
		$this->smarty->display("cyitem/mylove.html");
	}
	
	
	public function onMyJoin(){
		 
		$userid=M("login")->userid;
		$limit=20;
		$start=get_post("per_page","i");
		$option=array(
			"limit"=>$limit,
			"start"=>$start,
			"order"=>" id DESC",
			"where"=>" userid=".$userid."  ",
		);
		$rscount=true;
		$data=M("cyitem_join")->select($option,$rscount);
		 
		if($data){
			foreach($data as $k=>$v){
				$a=M("cyitem")->selectRow(" id=".$v['cyid']."");
				$sdata[]=$a;
			}
		}
		$url="/index.php?m=cyitem&a=mylove";
		$pagelist=$this->pagelist($rscount,$limit,$url); 
		$this->smarty->goassign(array(
			"data"=>$sdata,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist,
			"item_status_list"=>M("cyitem")->item_status_list()
		));
		$tpl=M("pagetpl")->get("cyitem","myjoin");
		$this->smarty->display($tpl);
		$this->smarty->display("cyitem/myjoin.html");
	}
	
	/*发表创业项目模型*/
	public function onAdd(){
		$cat_list=M("category")->children(0,MODEL_CYITEM_ID);;
		$id=get('id','i');
		if($id){
			$data=M("cyitem")->selectRow(array("where"=>"id=$id"));
			if($data['userid']!=M("login")->userid){
				$this->goAll("暂无权限",1);
			}
			$t_d=M("cyitem_data")->selectRow(array("where"=>"id=$id"));
			if(!empty($t_d)){
				$data=array_merge($data,$t_d);
			}		
		}else{
			$data=M("cyitem")->selectRow(array("where"=>"is_temp=1 AND userid=".M("login")->userid." "));
			if(empty($data)){
				$id=M("model_index")->insert(array("tablename"=>"cyitem"));
				M("cyitem")->insert(array("id"=>$id,"dateline"=>time(),"is_temp"=>1,"status"=>99,"siteid"=>SITEID,"userid"=>M("login")->userid));
				M("cyitem_data")->insert(array("id"=>$id,"dateline"=>time()));
				$data['id']=$id;
			}
		}
		$this->smarty->goassign(
			array(
				"cat_list"=>$cat_list,
				"data"=>$data,
				"user_cat_list"=>M("user_category")->children(array("where"=>" tablename='cyitem' AND pid=0 AND userid=".M("login")->userid)),
				"item_status_list"=>M("cyitem")->item_status_list()
			)
		);
		$tpl=M("pagetpl")->get("cyitem","add");
		$this->smarty->display($tpl);
		$this->smarty->display("cyitem/add.html");
	}
	
	public function onSave(){
		$id=post('id','i');
		$endtime=strtotime(post('endtime'));
		$data=M("cyitem")->postData();
		$data['siteid']=SITEID;
		if($id){
			$row=M("cyitem")->selectRow("id=".$id);
			if($row['is_temp']==1){
				$data['is_temp']=0;
				$data['status']=1;
			}
			if($row['userid']!=M("login")->userid) $this->goall($this->lang['die_access'],1,0,"/index.php");
		}
		
		 
		$data['last_time']=time();			
	 
		$cat=M("category")->selectRow("catid=".$data['catid']);
		if(empty($cat)) $this->goall($this->lang['cat_empty'],1);
		 
		if($data['imgurl']){
			$data['is_img']=1;
		}else{
			$data['is_img']=0;
		}			
		if($data['imgurl']){			 
			$data['is_img']=1;			 
		}
		
		//gps信息
		if(post('latlng')){
			$latlng=explode(",",post('latlng'));
			$data['lat']=round($latlng[0],6);
			$data['lng']=round($latlng[1],6);
			if ($data['lat'] && $data['lng']){
					$data['isgps']=1;
			}
		}
		$sdata=array(
			"id"=>$id,
			"content"=>post('content','x'),
			"dateline"=>time()
		);
		/*扩展信息*/
		if($row['status']==1){
			$data['money']=max(post('money','i'),1);
			$data['price']=max(1,post('price','i'));
			$data['total_num']=intval(max(1,$data['money']/$data['price']));
			$data['gufen_num']=post('gufen_num','i');
			 
		}
		if($row['status']!==1){
			$data['status']=3;
		}
		 
		$data['endtime']=$endtime;
		 
		/*END 扩展信息*/
		if($id){			
			M("cyitem")->update($data,array("id"=>$id));
			if(!M("cyitem_data")->selectRow("id=$id")){
				M("cyitem_data")->insert($sdata);
			}else{
				M("cyitem_data")->update($sdata,array("id"=>$id));
			}
		}else{
			$data['userid']=M("login")->userid;
			$data['dateline']=time();
			
			$data['id']=$id=M("model_index")->insert(array("tablename"=>"cyitem"));
			$sdata['id']=$id;
			if(M("cyitem")->insert($data)){
				M("cyitem_data")->insert($sdata);
			}
		}
		//更新相关统计
		if($row['is_temp']==1){
			M("user")->changeNum("topic_num",1,"userid=".$this->userid);
			$user=M("login")->getUser();
			M("category")->update_new_topic($data['catid'],array("last_time"=>time(),"nickname"=>$user['nickname'],"userid"=>$this->userid,"title"=>$data['title'],"id"=>$id));
		}
		$this->goall($this->lang['save_success'],0,$data,APPINDEX."?m=".get_post('m','h')."&a=add&id=".$id);
	}
	
	public function onDelete(){
		$row=M("cyitem")->selectRow("id=".$id);
		if($row['userid']!=M("login")->userid) $this->goall($this->lang['die_access'],1,0,"/index.php");
		M("cyitem")->update(array("status"=>98),"id=".$id);
		$this->goall($this->lang['delete_success']);
	}
	
	public function onAddClick(){
		$id=get_post('id','i');
		$row=M("cyitem")->selectRow("id=".$id);
		if($row){
			M("cyitem")->update(array("view_num"=>$row['view_num']+1),"id=".$id);
		}
		 
	 }
	
}
?>